﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DLV.Data.Entities;
using DLV.FrameWork;
using DLV.Library.Utilities;

namespace DLV.BackEnd.Pages
{
    public partial class ChietTinhAnUong : System.Web.UI.Page
    {
        protected List<CalculatorInfo> tourMenu = new List<CalculatorInfo>();
        protected List<CalculatorInfo> tourVisa = new List<CalculatorInfo>();
        public CalculatorInfo tourDVK = new CalculatorInfo();
        protected decimal TotalMenuPrices = 0;
        protected decimal TotalMenuAdults = 0;
        protected decimal TotalMenuChild = 0;

        protected decimal TotalVisaPrices = 0;
        protected decimal TotalVisaAdults = 0;
        protected decimal TotalVisaChild = 0;

        protected decimal TotalDVKPrices = 0;
        protected decimal TotalDVKAdults = 0;
        protected decimal TotalDVKChild = 0;

        protected decimal TotalHHPrices = 0;
        protected decimal TotalHHAdults = 0;
        protected decimal TotalHHChild = 0;

        private List<CalculatorInfo> _calculatorList = new List<CalculatorInfo>();
        private readonly CalculatorController _calculatorController = new CalculatorController();
        public List<CalculatorInfo> _calculatorListDvk = new List<CalculatorInfo>();
        public List<CalculatorInfo>  _calculatorListHH = new List<CalculatorInfo>();
        private readonly CalculatorController _calculatorControllerDvk = new CalculatorController();
        protected TourInfo tourInfo = new TourInfo();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Request.QueryString["TourId"]))
            {
                _calculatorList =
                    _calculatorController.ListCalculator()
                        .Where(p => p.TourId == int.Parse(Request.QueryString["TourId"])).ToList();
                tourInfo = new TourController().GetTour(int.Parse(Request.QueryString["TourId"]));
                _calculatorListDvk =
                    _calculatorControllerDvk.ListCalculatorDvk()
                        .Where(p => p.TourId == int.Parse(Request.QueryString["TourId"]) && p.ItemType == 8).ToList();
                _calculatorListHH =
                  _calculatorControllerDvk.ListCalculatorDvk()
                      .Where(p => p.TourId == int.Parse(Request.QueryString["TourId"]) && p.ItemType == 11).ToList();

            }
            LoadAddressDefault();
            LoadNational();
            LoadRestaurantType();
            LoadMenuType();
            //Visa
            LoadPartner();
            LoadVisaType();
            //Dich Vu Khac
            LoadLocationType();
            LoadNationalDVK();

            if (_calculatorList.Count == 0) return;
            RenderForm();
        }
        public void RenderForm()
        {

            if (!String.IsNullOrEmpty(Request.QueryString["TourId"]))
            {
                //Menu
                tourMenu = _calculatorList.Where(p => p.ItemType == 4).ToList();
                TotalMenuAdults = tourMenu.Sum(p => ((p.PriceAdults * p.QuantityAdults) + (p.PriceAdults * p.QuantityAdults) * Convert.ToDecimal(p.Vat / 100)));
                TotalMenuChild = tourMenu.Sum(p => ((p.PriceChild * p.QuantityChild) + (p.PriceChild * p.QuantityChild) * Convert.ToDecimal(p.Vat / 100)));
                TotalMenuPrices = TotalMenuChild + TotalMenuAdults;
                //Visa
                tourVisa = _calculatorList.Where(p => p.ItemType == 6).ToList();
                TotalVisaChild = tourVisa.Sum(p => p.TotalPriceVisaChild);
                TotalVisaAdults = tourVisa.Sum(p => p.TotalPriceVisaAdults);
                TotalVisaPrices = TotalVisaChild + TotalVisaAdults;  
                //hoa hoong 
                TotalHHAdults = _calculatorListHH.Sum(p => p.TotalPriceDvkAdults);
                TotalHHChild = _calculatorListHH.Sum(p => p.TotalPriceDvkChild);
                TotalHHPrices = TotalHHAdults + TotalHHChild; 
            }
        }
        private void LoadAddressDefault()
        {
            ddlNationalMenu.Items.Insert(0, new ListItem("--- Chọn quốc gia ---", "-1"));
            ddlCityMenu.Items.Insert(0, new ListItem("--- Chọn tỉnh/thành phố ---", "-1"));
            ddlRestaurantMenu.Items.Insert(0, new ListItem("--- Chọn nhà hàng ---", "-1"));
            ddlRestaurantTypeMenu.Items.Insert(0, new ListItem("--- Chọn loại nhà hàng ---", "-1"));
            ddlMenuMenu.Items.Insert(0, new ListItem("--- Chọn thực đơn ---", "-1"));
            ddlMenuTypeMenu.Items.Insert(0, new ListItem("--- Chọn loại thực đơn ---", "-1"));

            ddlVisaNameVisa.Items.Insert(0, new ListItem("--Chọn Visa --", "-1"));
        }
        public void LoadNational()
        {
            ddlNationalMenu.DataSource = new NationalController().ListNational().OrderBy(p => p.NationalName);
            ddlNationalMenu.DataValueField = "NationalId";
            ddlNationalMenu.DataTextField = "NationalName";
            ddlNationalMenu.DataBind();
            ddlNationalMenu.Items.Insert(0, new ListItem("--Chọn quốc gia--", "-1"));
        }

        private void LoadRestaurantType()
        {
            ddlRestaurantTypeMenu.DataSource = new RestaurantTypeController().ListRestaurantType()
                .Where(p => p.Status == (int)Globals.DocumentStatus.Use)
                .OrderBy(p => p.RestaurantTypeName).ToList();
            ddlRestaurantTypeMenu.DataTextField = "RestaurantTypeName";
            ddlRestaurantTypeMenu.DataValueField = "RestaurantTypeId";
            ddlRestaurantTypeMenu.DataBind();
            ddlRestaurantTypeMenu.Items.Insert(0, new ListItem("--- Chọn loại nhà hàng ---", "-1"));
        }
        private void LoadMenuType()
        {
            ddlMenuTypeMenu.DataSource = new MenuTypeController().ListMenuType()
                .Where(p => p.Status == (int)Globals.DocumentStatus.Use)
                .OrderBy(p => p.MenuTypeName);
            ddlMenuTypeMenu.DataTextField = "MenuTypeName";
            ddlMenuTypeMenu.DataValueField = "MenuTypeId";
            ddlMenuTypeMenu.DataBind();
            ddlMenuTypeMenu.Items.Insert(0, new ListItem("--- Chọn loại menu ---", "-1"));
        }
        //Visa
        public void LoadPartner()
        {
            ddlPartnerNameVisa.DataSource = new PartnerController().ListPartner().OrderBy(p => p.PartnerName);
            ddlPartnerNameVisa.DataValueField = "PartnerId";
            ddlPartnerNameVisa.DataTextField = "PartnerName";
            ddlPartnerNameVisa.DataBind();
            ddlPartnerNameVisa.Items.Insert(0, new ListItem("--Chọn nhà cung cấp --", "-1"));
        }
        public void LoadVisaType()
        {
            ddlVisaTypeNameVisa.DataSource = new VisaTypeController().ListVisaType().OrderBy(p => p.VisaTypeName);
            ddlVisaTypeNameVisa.DataValueField = "VisaTypeId";
            ddlVisaTypeNameVisa.DataTextField = "VisaTypeName";
            ddlVisaTypeNameVisa.DataBind();
            ddlVisaTypeNameVisa.Items.Insert(0, new ListItem("--Chọn  loại Visa --", "-1"));
        }

        //Dich vu khac
        public void LoadLocationType()
        {
            ddlLocationTypeDVK.DataSource = new LocationTypeController().ListLocationType().OrderBy(p => p.LocationTypeName);
            ddlLocationTypeDVK.DataValueField = "LocationTypeId";
            ddlLocationTypeDVK.DataTextField = "LocationTypeName";
            ddlLocationTypeDVK.DataBind();
            ddlLocationTypeDVK.Items.Insert(0, new ListItem("--Chọn  loại Visa --", "-1"));
        }

        public void LoadNationalDVK()
        {
            ddlNationalDVK.DataSource = new NationalController().ListNational().OrderBy(p => p.NationalName);
            ddlNationalDVK.DataValueField = "NationalId";
            ddlNationalDVK.DataTextField = "NationalName";
            ddlNationalDVK.DataBind();
            ddlNationalDVK.Items.Insert(0, new ListItem("--Chọn quốc gia--", "-1"));
        }
    }
}